<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    	<title>统战成员学习平台</title>
	    <link rel="stylesheet" type="text/css" href="../css/base.css" />
	    <link rel="stylesheet" type="text/css" href="../css/style.css" />
	</head>
	<body>
		<div id='app' style="display:none" v-show="true">
			<header v-if="!isLogin" class="red">
				<div class="ui_border_b">
					<p>课程</p>
				</div>
			</header>
			<div v-if="isLogin" class="top_nav red">
					<div @click="selectType(i, $event)" v-for="(item, i) of types" :class="{'active': i === activeIndex}"><span >{{item.name}}</span></div>
			</div>
			<div v-if="!isLogin" class="no_lesson" :style="{'height': height + 'px','padding-top': '5rem'}">
				<img src="../image/no_lesson_bg.png">
				<p class="tip">您还没有登录，请登陆后查看</p>
				<p @click="pageUtil.toLogin()" class="see">登录</p>
			</div>
		</div>
		<script type="text/javascript" src="../script/api.js"></script>
		<script type="text/javascript" src="../script/vue.min.js"></script>
		<script type="text/javascript" src="../script/common.js"></script>
		<script type="text/javascript" src="../script/layer_mobile/layer.js"></script>
		<script type="text/javascript">
		    new Vue({
		     	el: '#app',
		     	data: {
					click : false,
					scrollWidth: 1000,
					activeIndex: 0,
					types: [{url: '/client/coursefront/queryNewCourse', name: '最新课程'},
							{url: '/client/coursefront/queryHotCourse', name: '热门课程'},
							{url: '/client/coursefront/querySuperiorCourse', name: '精品课程'}],
					isLogin: false,
					height: 500,
					translateX: 20
		     	},
		     	mounted: function() {
		        	var $this = this;
		        	apiready = function() {
						// hide this frame
						api.setFrameAttr({
						    name: api.frameName,
						    hidden: true
						});
						$this.init();
						$this.addEventListener();
						api.addEventListener({
						    name: 'user_login_logout_event'
						}, function(ret, err){
							$this.init();
						});
                	}
		    	},
		    	methods: {
					init: function() {
			        	var $this = this;
						var userInfo = $api.getStorage(_.USERINFO);
						$this.isLogin = !!userInfo;
						setTimeout(function() {
							// fix header
							var header = document.querySelector($this.isLogin ? '.top_nav' : 'header');
							$api.fixStatusBar(header);
							$this.height = api.frameHeight - $api.offset(header).h;
							if(!$this.isLogin) {
								api.closeFrameGroup({
									name: 'lessonGroup'
								});
							} else {
								$this.calcWidth();
							}
						}, 1000);
					},
					addEventListener: function() {
			        	var $this = this;
						// open FrameGroup
						api.addEventListener({
							name: 'open_lesson_group_event'
						}, function(ret, err){
							setTimeout(function() {
								$this.openFrameGroup($this)
							}, 0)
						});
						// hide framegroup
						api.addEventListener({
							name: 'hide_lesson_group_event'
						}, function(ret, err){
							api.setFrameGroupAttr({
								name: 'lessonGroup',
								hidden: true
							});
						});
					},
					openFrameGroup: function($this) {
						var header = document.querySelector($this.isLogin ? '.top_nav' : 'header');
						api.openFrameGroup ({
							name: 'lessonGroup',
							preload: 2,
							rect: {
								 x: 0,
								 y: $api.offset(header).h + 10,
								 w: 'auto',
								 h: api.frameHeight - $api.offset(header).h - 10
							},
							frames: $this.types.map(function(item) {
								return {
									bounces: true,
									name: 'frame' + item.id,
									url: 'lesson_frame.html',
									pageParam: {
										url: item.url
									}
								}
							})
						}, function(ret, err){
							$this.click ? $this.click = false : $this.activeIndex = ret.index;

							$this.calcScroll(ret.index)
						});
						api.setFrameGroupAttr({
							name: 'lessonGroup',
							hidden: false
						});
					},
					calcWidth: function() {
						// calcWidth

					},
					calcScroll: function(i) {

					},
					selectType: function(i, $event){
						var $this = this;
						if($this.activeIndex === i) return;
						$this.translateX = $event.target.offsetLeft + ($event.target.offsetWidth - 40) / 2;
						$this.click = true;
						$this.activeIndex = i;
						api.setFrameGroupIndex({
							name : 'lessonGroup',
							scroll: true,
							index : i
						});
						$this.calcScroll(i)
					}
		    	}
		    });
		</script>
	</body>
</html>
